package easy;

public class Solution_1022 {

    private int sum = 0;

    public int sumRootToLeaf(TreeNode root) {
        dps(root, 0);
        return sum;
    }

    private void dps(TreeNode node, int num) {
        int n = (num << 1) + node.val;
        if (node.left == null && node.right == null) {
            sum = sum + n;
            return;
        }
        if (node.left != null) {
            dps(node.left, n);
        }
        if (node.right != null) {
            dps(node.right, n);
        }
    }

    public static void main(String[] args) {
        Solution_1022 model = new Solution_1022();
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(0);
        root.left.left = new TreeNode(0);
        root.left.right = new TreeNode(1);
        root.right = new TreeNode(1);
        root.right.left = new TreeNode(0);
        root.right.right = new TreeNode(1);
        System.out.println(model.sumRootToLeaf(root));
    }

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
